package com.example.administrator.about_huanxin.presenter.activity;

import android.util.Log;

import com.example.administrator.about_huanxin.SimpelEmCallBack;
import com.example.administrator.about_huanxin.model.UserEntity;
import com.example.administrator.about_huanxin.view.activity.RegisterActivityView;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UploadFileListener;

/**
 * Created by Administrator on 2017/3/10.
 */
public class RegisiterPresenterImp implements RegisiterPresenter {

    private RegisterActivityView registerActivityView;

    public RegisiterPresenterImp(RegisterActivityView registerActivityView) {
        this.registerActivityView = registerActivityView;
    }

    @Override
    public void uploadUserIcon(final UserEntity user) {
        //上传头像
        user.getIcon().uploadblock(new UploadFileListener() {
            @Override
            public void done(BmobException e) {
                if(e == null){
                    //success
                    registerActivityView.afterUploadIcon(true);
                    saveUserInfoToBmob(user);
                }else{
                    //fail
                    Log.e("msg",e.getMessage()+"...");
                    registerActivityView.afterUploadIcon(false);
                }
            }
        });
    }

    //保存用户信息
    private void saveUserInfoToBmob(final UserEntity userEntity){
        userEntity.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if(e == null){
                    registerActivityView.afterSaveUserInfo(true);
                    createEmAccount(userEntity);
                }else{
                    registerActivityView.afterSaveUserInfo(false);
                    Log.e("msg",e.getMessage()+"...");
                }
            }
        });
    }

    //环信上创建账户
    private void createEmAccount(final UserEntity userEntity){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //该方法是同步方法(有联网操作)，所以要放在线程中完成
                    EMClient.getInstance().createAccount(userEntity.getUsername(),userEntity.getPassword());
                    registerActivityView.afterLoginEMClient(true);
//                    LoginEmServer(userEntity);
                } catch (HyphenateException e) {
                    //失败会到这
                    registerActivityView.afterLoginEMClient(false);
                    e.printStackTrace();
                }
            }
        }).start();
    }

//    //登录环信的服务器
//    private void LoginEmServer(UserEntity userEntity){
//        EMClient.getInstance().login(userEntity.getUsername(),userEntity.getPassword(),new SimpelEmCallBack(){
//
//            @Override
//            public void onSuccess() {
//                super.onSuccess();
//                registerActivityView.afterFinalLoginEmClient(true);
//            }
//
//            @Override
//            public void onError(int i, String s) {
//                super.onError(i, s);
//                Log.e("msg",s+"...");
//                registerActivityView.afterFinalLoginEmClient(false);
//            }
//        });
//    }

}
